//
//  LocalTime.m
//  xpkc
//
//  Created by wanmeizty on 14-9-1.
//  Copyright (c) 2014年 wanmeizty. All rights reserved.
//

#import "LocalTime.h"

@implementation LocalTime

+(void)saveTheTime:(double)time withVideoID:(NSString *)videoId{
    NSNumber *localTime = [NSNumber numberWithDouble:time];
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:PLAYEDTIME_PATH];
    NSMutableArray * array = [dic objectForKey:@"times"];
    if (array == nil) {
        array = [[NSMutableArray alloc]init];
    }
    BOOL hadtime = NO;
    int index = 0;
    NSDictionary *newDic = [NSDictionary dictionaryWithObjectsAndKeys:videoId,@"videoId",localTime,@"time", nil];
    for (NSDictionary *timedic in array) {
        if ([videoId isEqualToString:[timedic objectForKey:@"videoId"]]) {
            //
            hadtime = YES;
            break;
        }
        index++;
    }
    if (hadtime) {
        [array replaceObjectAtIndex:index withObject:newDic];
    }else{
        [array addObject:newDic];
    }
    NSDictionary *writeDic = [NSDictionary dictionaryWithObjectsAndKeys:array,@"times", nil];
    [writeDic writeToFile:PLAYEDTIME_PATH atomically:YES];
    logdebug(@"时间已保存");
}

+(double)getThePlayedTimeWithVideoID:(NSString *)videoId{
    logdebug(@"获取上次时间");
    //获取上次播放时间
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:PLAYEDTIME_PATH];
    NSArray *array = [dic objectForKey:@"times"];
    for (NSDictionary *timedic in array) {
        if ([[timedic objectForKey:@"videoId"]isEqualToString:videoId]) {
            //有就返回时间
            return [[timedic objectForKey:@"time"]doubleValue];
            break;
        }
    }
    //没有就返回0
    return 0.0;
}
@end
